<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import { useUserStore } from '../../stores/userStore'
import axios from '../../axios/axiosHelper'
import { useRouter } from 'vue-router'

// 获取用户状态管理和路由
const userStore = useUserStore()
const router = useRouter()

// 表单数据
const loginForm = reactive({
  name: '',
  password: ''
})

// 表单验证规则
const rules = {
  name: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}

// 表单引用
const loginFormRef = ref(null)

// 加载状态
const loading = ref(false)

// 登录方法
const handleLogin = () => {
  loginFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        // 调用后端登录接口
        const response = await axios.post('/user/login', null, {
          params: {
            name: loginForm.name,
            password: loginForm.password
          }
        })
        
        // 处理登录响应
        if (response.data.code === 200) {
          // 登录成功
          ElMessage.success(response.data.msg || '登录成功')
          
          // 保存用户信息到本地存储
          localStorage.setItem('user', JSON.stringify(response.data.data))
          localStorage.setItem('token', 'token-' + Date.now())
          
          // 跳转到demo页面
          router.push('/index/demo')
        } else {
          // 登录失败
          ElMessage.error(response.data.msg || '用户名或密码错误')
        }
      } catch (error) {
        console.error('登录错误:', error)
        ElMessage.error('登录失败: ' + (error.message || '未知错误'))
      } finally {
        loading.value = false
      }
    } else {
      ElMessage.error('请正确填写登录信息')
      return false
    }
  })
}
</script>

<template>
  <div class="login-container">
    <el-card class="login-card">
      <template #header>
        <div class="card-header">
          <h2>系统登录</h2>
        </div>
      </template>
      
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="rules"
        label-position="top"
      >
        <el-form-item prop="name" label="用户名">
          <el-input
            v-model="loginForm.name"
            placeholder="请输入用户名"
            :prefix-icon="User"
            clearable
          />
        </el-form-item>
        
        <el-form-item prop="password" label="密码">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            :prefix-icon="Lock"
            show-password
          />
        </el-form-item>
        
        <!-- 记住我和忘记密码功能已移除 -->
        
        <el-form-item>
          <el-button
            type="primary"
            :loading="loading"
            @click="handleLogin"
            class="login-button"
          >
            登录
          </el-button>
        </el-form-item>
        
        <!-- 注册功能已移除 -->
      </el-form>
    </el-card>
  </div>
</template>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #e8f5e9; /* 浅绿色背景 */
  background-image: linear-gradient(to bottom right, #e8f5e9, #c8e6c9); /* 渐变效果 */
}

.login-card {
  width: 400px;
  border-radius: 8px;
  box-shadow: 0 4px 16px 0 rgba(76, 175, 80, 0.1); /* 绿色阴影 */
  border: 1px solid rgba(76, 175, 80, 0.1); /* 浅绿色边框 */
  background-color: rgba(255, 255, 255, 0.9); /* 半透明白色背景 */
}

.card-header {
  text-align: center;
  color: #2e7d32; /* 深绿色文字 */
  border-bottom: 1px solid #e8f5e9; /* 浅绿色分隔线 */
  padding-bottom: 10px;
  margin-bottom: 10px;
}

/* 记住我和忘记密码样式已移除 */

.login-button {
  width: 100%;
  padding: 12px 0;
  font-size: 16px;
  border-radius: 4px;
  background-color: #4caf50; /* 绿色按钮 */
  border-color: #4caf50;
}

.login-button:hover {
  background-color: #43a047; /* 深绿色悬停效果 */
  border-color: #43a047;
}

/* 注册链接样式已移除 */
</style>